<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>在Unity中使用async和await | Hotaru Hikari's Blog</title><meta name="keywords" content="Unity,C#"><meta name="author" content="螢の光"><meta name="copyright" content="螢の光"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content=":::info前言：记录async和await的基本用法，如何去替代协程。::: 使用async、await代替Coroutine示例一首先通过一个延时的例子来了解其基本用法。 12345678910111213141516&#x2F;&#x2F;使用await的延时写法async Task delayTime2()&amp;#123;    Debug.Log(&quot;Async start&quot;);    &#x2F;&#x2F;">
<meta property="og:type" content="article">
<meta property="og:title" content="在Unity中使用async和await">
<meta property="og:url" content="https://hotarunohikari.gitee.io/hotaru-no-hikari/2021/10/28/%E4%BD%BF%E7%94%A8await%E3%80%81aysnc/index.html">
<meta property="og:site_name" content="Hotaru Hikari&#39;s Blog">
<meta property="og:description" content=":::info前言：记录async和await的基本用法，如何去替代协程。::: 使用async、await代替Coroutine示例一首先通过一个延时的例子来了解其基本用法。 12345678910111213141516&#x2F;&#x2F;使用await的延时写法async Task delayTime2()&amp;#123;    Debug.Log(&quot;Async start&quot;);    &#x2F;&#x2F;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://hotarunohikari.gitee.io/hotaru-no-hikari/img/cover2.jpg">
<meta property="article:published_time" content="2021-10-28T08:31:50.000Z">
<meta property="article:modified_time" content="2021-10-28T09:08:52.113Z">
<meta property="article:author" content="螢の光">
<meta property="article:tag" content="Unity">
<meta property="article:tag" content="C#">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://hotarunohikari.gitee.io/hotaru-no-hikari/img/cover2.jpg"><link rel="shortcut icon" href="/hotaru-no-hikari/img/avatar.jpg"><link rel="canonical" href="https://hotarunohikari.gitee.io/hotaru-no-hikari/2021/10/28/%E4%BD%BF%E7%94%A8await%E3%80%81aysnc/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/hotaru-no-hikari/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/hotaru-no-hikari/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '在Unity中使用async和await',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-10-28 17:08:52'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/style.css"><link rel="stylesheet" href="/css/remixicon.css"><meta name="generator" content="Hexo 6.2.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/hotaru-no-hikari/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/hotaru-no-hikari/archives/"><div class="headline">文章</div><div class="length-num">9</div></a><a href="/hotaru-no-hikari/tags/"><div class="headline">标签</div><div class="length-num">9</div></a><a href="/hotaru-no-hikari/categories/"><div class="headline">分类</div><div class="length-num">13</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/"><i class="fa-fw ri-home-wifi-line"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/bangumis/"><i class="fa-fw fas fa-folder-open"></i><span> 追番</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 链接</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hotaru-no-hikari/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></li><li><a class="site-page child" href="/hotaru-no-hikari/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/hotaru-no-hikari/img/cover2.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/hotaru-no-hikari/">Hotaru Hikari's Blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/"><i class="fa-fw ri-home-wifi-line"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/hotaru-no-hikari/bangumis/"><i class="fa-fw fas fa-folder-open"></i><span> 追番</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 链接</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hotaru-no-hikari/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></li><li><a class="site-page child" href="/hotaru-no-hikari/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">在Unity中使用async和await</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-10-28T08:31:50.000Z" title="发表于 2021-10-28 16:31:50">2021-10-28</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-10-28T09:08:52.113Z" title="更新于 2021-10-28 17:08:52">2021-10-28</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/hotaru-no-hikari/categories/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/">游戏开发</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/hotaru-no-hikari/categories/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/Unity/">Unity</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">580</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>2分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="在Unity中使用async和await"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>:::info<br>前言：记录async和await的基本用法，如何去替代协程。<br>:::</p>
<h1 id="使用async、await代替Coroutine"><a href="#使用async、await代替Coroutine" class="headerlink" title="使用async、await代替Coroutine"></a>使用async、await代替Coroutine</h1><h2 id="示例一"><a href="#示例一" class="headerlink" title="示例一"></a>示例一</h2><p>首先通过一个延时的例子来了解其基本用法。</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用await的延时写法</span></span><br><span class="line"><span class="function"><span class="keyword">async</span> Task <span class="title">delayTime2</span>()</span></span><br><span class="line">&#123;</span><br><span class="line">    Debug.Log(<span class="string">&quot;Async start&quot;</span>);</span><br><span class="line">    <span class="comment">//await Task.Delay(2000); //2000ms</span></span><br><span class="line">    <span class="keyword">await</span> Task.Delay(TimeSpan.FromSeconds(<span class="number">2f</span>));</span><br><span class="line">    Debug.Log(<span class="string">&quot;Async end&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用协程的延时写法</span></span><br><span class="line"><span class="function">IEnumerator <span class="title">delayTime1</span>()</span></span><br><span class="line">&#123;</span><br><span class="line">    Debug.Log(<span class="string">&quot;Coroutine start&quot;</span>);</span><br><span class="line">    <span class="function"><span class="keyword">yield</span> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title">WaitForSeconds</span>(<span class="params"><span class="number">2.0f</span></span>)</span>;</span><br><span class="line">    Debug.Log(<span class="string">&quot;Coroutine end&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>##示例二<br>使控制的游戏物体依次选择，title在所有操作执行完后显示。</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">async</span> <span class="keyword">void</span> <span class="title">StartTest</span>()</span></span><br><span class="line">&#123;</span><br><span class="line">    title.gameObject.SetActive(<span class="literal">false</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; gameObject.Length; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">await</span> gameObject[i].RotateForSeconds(<span class="number">1</span> + <span class="number">1</span> * i);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    title.gameObject.SetActive(<span class="literal">true</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">async</span> Task <span class="title">RotateForSeconds</span>(<span class="params"><span class="built_in">float</span> d</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">var</span> end = Time.time + d;</span><br><span class="line">    <span class="keyword">while</span> (Time.time &lt; end)</span><br><span class="line">    &#123;</span><br><span class="line">        transform.Rotate(<span class="keyword">new</span> Vector3(<span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>) * Time.deltaTime * <span class="number">150</span>);</span><br><span class="line">        <span class="keyword">await</span> Task.Yield();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>在上面的基础上改为物体一起旋转。并不是每一步都通过await等待，在最后执行<code>await Task.WhenAll(tasks)</code>等待所有Task执行结束。</li>
<li>同理，这个对应旋转的Task可以换成其它复杂逻辑实现对应的效果。</li>
</ul>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">async</span> <span class="keyword">void</span> <span class="title">StartTest</span>()</span></span><br><span class="line">&#123;</span><br><span class="line">    title.gameObject.SetActive(<span class="literal">false</span>);</span><br><span class="line">    Task[] tasks = <span class="keyword">new</span> Task[<span class="number">3</span>];</span><br><span class="line">    <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; gameObject.Length; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        tasks[i] = gameObject[i].RotateForSeconds(<span class="number">1</span> + <span class="number">1</span> * i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">await</span> Task.WhenAll(tasks);</span><br><span class="line">    title.gameObject.SetActive(<span class="literal">true</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h1 id="带有返回值"><a href="#带有返回值" class="headerlink" title="带有返回值"></a>带有返回值</h1><ul>
<li>协程没有返回值，但异步方法可以带有返回值。<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">async</span> Task&lt;<span class="built_in">int</span>&gt; <span class="title">GetRandomNum</span>(<span class="params"><span class="built_in">int</span> max</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    System.Random random = <span class="keyword">new</span> System.Random();</span><br><span class="line">    <span class="built_in">int</span> res = random.Next(<span class="number">0</span>, max + <span class="number">1</span>);</span><br><span class="line">    <span class="keyword">await</span> Task.Delay(TimeSpan.FromSeconds(<span class="number">1f</span>));</span><br><span class="line">    <span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">async</span> Task <span class="title">delayTime2</span>()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">var</span> task = <span class="keyword">await</span> GetRandomNum(<span class="number">100</span>);</span><br><span class="line">    Debug.Log(<span class="built_in">string</span>.Format(<span class="string">&quot;Result:&#123;0&#125;&quot;</span>, task));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h1 id="GetAwaiter拓展"><a href="#GetAwaiter拓展" class="headerlink" title="GetAwaiter拓展"></a>GetAwaiter拓展</h1><ul>
<li>GetAwaiter拓展，允许等待任何想要的东西。</li>
<li>基于C#实现自定义Async&#x2F;Await的要求，需要完成一个实现了INotifyCompletion接口的Awaiter。</li>
<li>引用上<code>System.Runtime.CompilerServices</code>命名空间。</li>
</ul>
<p>案例如下：<br>如果不添加拓展，<code>await request.SendWebRequest()</code>是没法使用的，必须要对其实现一个GetAwaiter方法。</p>
<figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">async</span> Task <span class="title">delayTime2</span>()</span></span><br><span class="line">&#123;</span><br><span class="line">    UnityWebRequest request = UnityWebRequest.Get(<span class="string">&quot;path&quot;</span>);</span><br><span class="line">    <span class="keyword">await</span> request.SendWebRequest();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class</span> <span class="title">AwaitExtensions</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> TaskAwaiter <span class="title">GetAwaiter</span>(<span class="params"><span class="keyword">this</span> AsyncOperation asyncOperation</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">var</span> tcs = <span class="keyword">new</span> TaskCompletionSource&lt;<span class="built_in">object</span>&gt;();</span><br><span class="line">        asyncOperation.completed += obj =&gt; &#123; tcs.SetResult(<span class="literal">null</span>); &#125;;</span><br><span class="line">        <span class="keyword">return</span> ((Task)tcs.Task).GetAwaiter();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>相关链接如下：<br><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/362272520">如何实现Unity Async异步下载资源时显示进度</a><br><a target="_blank" rel="noopener" href="https://blog.csdn.net/lindexi_gd/article/details/100174692?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522158694895719195239842318%2522%252C%2522scm%2522%253A%252220140713.130056874..%2522%257D&request_id=158694895719195239842318&biz_id=0&utm_source=distribute.pc_search_result.none-task-blog-1~blog~default~default-1">C＃ await 高级用法</a><br><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/362272520">B站视频：【Unity】协程的火辣姐妹– 异步async await</a></p>
</blockquote>
<p><code>待补充。。</code></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://hotarunohikari.gitee.io/hotaru-no-hikari">螢の光</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://hotarunohikari.gitee.io/hotaru-no-hikari/2021/10/28/%E4%BD%BF%E7%94%A8await%E3%80%81aysnc/">https://hotarunohikari.gitee.io/hotaru-no-hikari/2021/10/28/%E4%BD%BF%E7%94%A8await%E3%80%81aysnc/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://hotarunohikari.gitee.io/hotaru-no-hikari" target="_blank">Hotaru Hikari's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/hotaru-no-hikari/tags/Unity/">Unity</a><a class="post-meta__tags" href="/hotaru-no-hikari/tags/C/">C#</a></div><div class="post_share"><div class="social-share" data-image="/hotaru-no-hikari/img/cover2.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/gh/overtrue/share.js@master/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/hotaru-no-hikari/2021/10/30/%E7%A7%8B%E6%8B%9B%E6%80%BB%E7%BB%93/"><img class="prev-cover" src="/hotaru-no-hikari/img/cover2.jpg" onerror="onerror=null;src='/hotaru-no-hikari/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">简单的秋招总结</div></div></a></div><div class="next-post pull-right"><a href="/hotaru-no-hikari/2021/10/27/%E5%9C%A8Unity%E4%B8%AD%E4%BD%BF%E7%94%A8System.Drawing%E7%94%9F%E6%88%90%E9%AA%8C%E8%AF%81%E7%A0%81/"><img class="next-cover" src="/hotaru-no-hikari/img/cover2.jpg" onerror="onerror=null;src='/hotaru-no-hikari/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">在Unity中使用Bitmap生成验证码图片</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/hotaru-no-hikari/2021/10/24/OpenCVForUnity/" title="OpenCV在Unity中的简单运用"><img class="cover" src="/hotaru-no-hikari/img/cover2.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-24</div><div class="title">OpenCV在Unity中的简单运用</div></div></a></div><div><a href="/hotaru-no-hikari/2021/10/24/%E5%88%9D%E6%AD%A5%E4%BA%86%E8%A7%A3%E7%BC%96%E8%BE%91%E5%99%A8%E6%8B%93%E5%B1%95/" title="初步了解编辑器拓展"><img class="cover" src="/hotaru-no-hikari/img/cover2.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-24</div><div class="title">初步了解编辑器拓展</div></div></a></div><div><a href="/hotaru-no-hikari/2021/10/27/%E5%9C%A8Unity%E4%B8%AD%E4%BD%BF%E7%94%A8System.Drawing%E7%94%9F%E6%88%90%E9%AA%8C%E8%AF%81%E7%A0%81/" title="在Unity中使用Bitmap生成验证码图片"><img class="cover" src="/hotaru-no-hikari/img/cover2.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-27</div><div class="title">在Unity中使用Bitmap生成验证码图片</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/hotaru-no-hikari/img/avatar.jpg" onerror="this.onerror=null;this.src='/hotaru-no-hikari/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">螢の光</div><div class="author-info__description">游戏开发者成长日志</div></div><div class="card-info-data site-data is-center"><a href="/hotaru-no-hikari/archives/"><div class="headline">文章</div><div class="length-num">9</div></a><a href="/hotaru-no-hikari/tags/"><div class="headline">标签</div><div class="length-num">9</div></a><a href="/hotaru-no-hikari/categories/"><div class="headline">分类</div><div class="length-num">13</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/HotaruHikari"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/HotaruHikari" target="_blank" title="Github"><i class="ri-home-wifi-line"></i></a><a class="social-icon" href="mailto:728831102@qq.com" target="_blank" title="Email"><i class="fa-solid fa-house-laptop"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8async%E3%80%81await%E4%BB%A3%E6%9B%BFCoroutine"><span class="toc-number">1.</span> <span class="toc-text">使用async、await代替Coroutine</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B%E4%B8%80"><span class="toc-number">1.1.</span> <span class="toc-text">示例一</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B8%A6%E6%9C%89%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-number">2.</span> <span class="toc-text">带有返回值</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#GetAwaiter%E6%8B%93%E5%B1%95"><span class="toc-number">3.</span> <span class="toc-text">GetAwaiter拓展</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/hotaru-no-hikari/2022/06/07/hello-world/" title="Hello World"><img src="/hotaru-no-hikari/img/cover2.jpg" onerror="this.onerror=null;this.src='/hotaru-no-hikari/img/404.jpg'" alt="Hello World"/></a><div class="content"><a class="title" href="/hotaru-no-hikari/2022/06/07/hello-world/" title="Hello World">Hello World</a><time datetime="2022-06-07T10:58:52.802Z" title="发表于 2022-06-07 18:58:52">2022-06-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/hotaru-no-hikari/2021/10/30/%E7%A7%8B%E6%8B%9B%E6%80%BB%E7%BB%93/" title="简单的秋招总结"><img src="/hotaru-no-hikari/img/cover2.jpg" onerror="this.onerror=null;this.src='/hotaru-no-hikari/img/404.jpg'" alt="简单的秋招总结"/></a><div class="content"><a class="title" href="/hotaru-no-hikari/2021/10/30/%E7%A7%8B%E6%8B%9B%E6%80%BB%E7%BB%93/" title="简单的秋招总结">简单的秋招总结</a><time datetime="2021-10-30T03:45:48.000Z" title="发表于 2021-10-30 11:45:48">2021-10-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/hotaru-no-hikari/2021/10/28/%E4%BD%BF%E7%94%A8await%E3%80%81aysnc/" title="在Unity中使用async和await"><img src="/hotaru-no-hikari/img/cover2.jpg" onerror="this.onerror=null;this.src='/hotaru-no-hikari/img/404.jpg'" alt="在Unity中使用async和await"/></a><div class="content"><a class="title" href="/hotaru-no-hikari/2021/10/28/%E4%BD%BF%E7%94%A8await%E3%80%81aysnc/" title="在Unity中使用async和await">在Unity中使用async和await</a><time datetime="2021-10-28T08:31:50.000Z" title="发表于 2021-10-28 16:31:50">2021-10-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/hotaru-no-hikari/2021/10/27/%E5%9C%A8Unity%E4%B8%AD%E4%BD%BF%E7%94%A8System.Drawing%E7%94%9F%E6%88%90%E9%AA%8C%E8%AF%81%E7%A0%81/" title="在Unity中使用Bitmap生成验证码图片"><img src="/hotaru-no-hikari/img/cover2.jpg" onerror="this.onerror=null;this.src='/hotaru-no-hikari/img/404.jpg'" alt="在Unity中使用Bitmap生成验证码图片"/></a><div class="content"><a class="title" href="/hotaru-no-hikari/2021/10/27/%E5%9C%A8Unity%E4%B8%AD%E4%BD%BF%E7%94%A8System.Drawing%E7%94%9F%E6%88%90%E9%AA%8C%E8%AF%81%E7%A0%81/" title="在Unity中使用Bitmap生成验证码图片">在Unity中使用Bitmap生成验证码图片</a><time datetime="2021-10-27T05:50:09.000Z" title="发表于 2021-10-27 13:50:09">2021-10-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/hotaru-no-hikari/2021/10/25/%E7%A0%B4%E6%99%93%E4%BC%A0%E8%AF%B4%E9%80%9A%E5%85%B3%E6%84%9F%E6%83%B3/" title="破晓传说通关感想"><img src="/hotaru-no-hikari/img/BG/3.jpg" onerror="this.onerror=null;this.src='/hotaru-no-hikari/img/404.jpg'" alt="破晓传说通关感想"/></a><div class="content"><a class="title" href="/hotaru-no-hikari/2021/10/25/%E7%A0%B4%E6%99%93%E4%BC%A0%E8%AF%B4%E9%80%9A%E5%85%B3%E6%84%9F%E6%83%B3/" title="破晓传说通关感想">破晓传说通关感想</a><time datetime="2021-10-25T12:45:48.000Z" title="发表于 2021-10-25 20:45:48">2021-10-25</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2022 By 螢の光</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/hotaru-no-hikari/js/utils.js"></script><script src="/hotaru-no-hikari/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>